﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectNocturnalFool
{
    public sealed class VirtualScreen
    {
        public int VirtualWidth;
        public int VirtualHeight;
        public float VirtualAspectRatio;

        public int PhysicalWidth { get; set; }
        public int PhysicalHeight { get; set; }
        
        public Rectangle area;

        private GraphicsDevice graphicsDevice;
        private RenderTarget2D screen;

        /// <summary>
        /// Allocate ourselves.
        /// We have a private constructor, so no one else can.
        /// </summary>
        static readonly VirtualScreen _instance = new VirtualScreen();

        /// <summary>
        /// Access SiteStructure.Instance to get the singleton object.
        /// Then call methods on that instance.
        /// </summary>
        public static VirtualScreen Instance
        {
            get { return _instance; }
        }

        public void initialize(int virtualWidth, int virtualHeight, GraphicsDevice graphicsDevice)
        {
            VirtualWidth = virtualWidth;
            VirtualHeight = virtualHeight;
            VirtualAspectRatio = (float)(virtualWidth) / (float)(virtualHeight);

            this.graphicsDevice = graphicsDevice;
            screen = new RenderTarget2D(graphicsDevice, virtualWidth, virtualHeight, false, graphicsDevice.PresentationParameters.BackBufferFormat, graphicsDevice.PresentationParameters.DepthStencilFormat, graphicsDevice.PresentationParameters.MultiSampleCount, RenderTargetUsage.DiscardContents);
        }

        public VirtualScreen()
        {
        }

        private bool areaIsDirty = true;

        public void PhysicalResolutionChanged()
        {
            areaIsDirty = true;
        }
        
        public void Update()
        {
            if (!areaIsDirty)
            {
                return;
            }

            areaIsDirty = false;
            PhysicalWidth = graphicsDevice.Viewport.Width;
            PhysicalHeight = graphicsDevice.Viewport.Height;
            var physicalAspectRatio = graphicsDevice.Viewport.AspectRatio;

            if ((int)(physicalAspectRatio * 10) == (int)(VirtualAspectRatio * 10))
            {
                area = new Rectangle(0, 0, PhysicalWidth, PhysicalHeight);
                return;
            }


            if (VirtualAspectRatio > physicalAspectRatio)
            {
                var scaling = (float)PhysicalWidth / (float)VirtualWidth;
                var width = (float)(VirtualWidth) * scaling;
                var height = (float)(VirtualHeight) * scaling;
                var borderSize = (int)((PhysicalHeight - height) / 2);
                area = new Rectangle(0, borderSize, (int)width, (int)height);
            }
            else
            {
                var scaling = (float)PhysicalHeight / (float)VirtualHeight;
                var width = (float)(VirtualWidth) * scaling;
                var height = (float)(VirtualHeight) * scaling;
                var borderSize = (int)((PhysicalWidth - width) / 2);
                area = new Rectangle(borderSize, 0, (int)width, (int)height);
            }
        }

        public void BeginCapture()
        {
            graphicsDevice.SetRenderTarget(screen);
        }

        public void EndCapture()
        {
            graphicsDevice.SetRenderTarget(null);
        }

        public void Draw()
        {
            TacticsGame.Instance.spriteBatch.Draw(screen, area, Color.White);
        }


    }
}
